<script type="text/javascript">
	
/*
    - 平衡二叉树表示根节点永远大于左子树，根节点永远小于右子树
*/
function VerifySquenceOfBST(sequence)
{
    let len = sequence.length,
        root = sequence[len-1];
    if(len < 1) return false;
    // 找出所有子树，用数组arrleft，arrright存放左右子树集合
    let index = 0;
    while(sequence[index] < root && index < len) {
        index++;
    }
    let arrLeft = sequence.slice(0, index),
        arrRight = sequence.slice(index, -1);
    // 遍历右子树中的数据，如果arrRight中存在小于根节点的数据，则返回false
    for(let i = 0; i < arrRight.length; i++) {
        if(arrRight[i] < root) return false;
    }
    // 递归上述步骤，分别递归arrLeft，arrRight
    // let res1 = VerifySquenceOfBST(arrLeft),
    //     res2 = VerifySquenceOfBST(arrRight);
    debugger
    let res1 = true, res2 = true;
    if(arrLeft.length >= 2) {
		res1 = VerifySquenceOfBST(arrLeft);
    } 
    if(arrRight.length >= 2) {
    	res2 = VerifySquenceOfBST(arrRight);
    }
    
    return res1 && res2;
}

const res = VerifySquenceOfBST([7,4,6,5]);
console.log(res);

</script>